﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Phone_Remote.ViewModels;

using Phone_Remote.FC;

namespace Phone_Remote
{
    public partial class MainPage : PhoneApplicationPage
    {
        static CPQClient _connection = new CPQClient();
        List<string> _on = new List<string>();
        List<string> _off = new List<string>();
        string _code = "WP7CallerID";

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            _connection.SetDefaultCRIOAddressAsync(_code);

            StopFountain();
            _connection.GetResourcesCompleted += new EventHandler<GetResourcesCompletedEventArgs>(_connection_GetResourcesCompleted);
            _connection.GetRestrictedValvesCompleted += new EventHandler<GetRestrictedValvesCompletedEventArgs>(_connection_GetRestrictedValvesCompleted);
        }

        void _connection_GetRestrictedValvesCompleted(object sender, GetRestrictedValvesCompletedEventArgs e)
        {
            int index = 0;
            switch ((string)e.UserState)
            {
                case "LMOC": index = 0;
                    break;
                case "music": index = 1;
                    break;
                case "kiosk": index = 2;
                    break;
                case "phone": index = 3;
                    break;
                case "bollards": index = 4;
                    break;
                case "patterns": index = 5;
                    break;
            }

            string valves = "";
            foreach (string s in (List<String>)e.Result)
            {
                valves += s + " ";
            }

            MainViewModel.SetRestricted(valves, index);
        }

        void _connection_GetResourcesCompleted(object sender, GetResourcesCompletedEventArgs e)
        {
            cRIOState state = e.Result;
            Color[] myColors = new Color[state.ReportedEnvironStates.Count];

            for (int i = 0; i < state.ReportedEnvironStates.Count; i++)
            {
                if (state.ReportedEnvironStates.ElementAt(i))
                    myColors[i] = Colors.Red;
                else
                    myColors[i] = Color.FromArgb(255, 0, 205, 102);
                    
            }

            MainViewModel.SetEnviron(new SolidColorBrush(myColors[0]), new SolidColorBrush(myColors[1]), new SolidColorBrush(myColors[2]), new SolidColorBrush(myColors[3]));
        }

        private void StopFountain()
        {
            List<string> cmd = new List<string>();
            cmd.Add("V*");
            cmd.Add("H*");

            _connection.SendCommandAsync("OFF", cmd, CommandPriority.phone, _code);


            _connection.GetResourcesAsync();
           
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void UpdateRestricted_Click(object sender, RoutedEventArgs e)
        {
            List<List<string>> restricted = MainViewModel.getRestrictedValves();

            restricted = RemoveBadValves(restricted);

            _connection.AddRestrictedValvesAsync(restricted[0], CommandPriority.lmoc, _code);
            _connection.AddRestrictedValvesAsync(restricted[1], CommandPriority.music, _code);
            _connection.AddRestrictedValvesAsync(restricted[2], CommandPriority.kiosk, _code);
            _connection.AddRestrictedValvesAsync(restricted[3], CommandPriority.phone, _code);
            _connection.AddRestrictedValvesAsync(restricted[4], CommandPriority.bollards, _code);
            _connection.AddRestrictedValvesAsync(restricted[5], CommandPriority.patterns, _code);
        }

        private List<List<string>> RemoveBadValves(List<List<string>> restricted)
        {
            List<string> validValves = new List<string>();
            List<List<string>> ret = new List<List<string>>();

            // list of valid valves
            for (int i = 1; i < 13; i++)
            {
                if (i < 11)
                {
                    validValves.Add("V" + i);
                    validValves.Add("H" + i);
                }
                else if (i == 11)
                {
                    validValves.Add("HC");
                    validValves.Add("VC");
                }
                else
                {
                    validValves.Add("VR");
                    validValves.Add("HR");
                }
            }

            foreach (List<string> l in restricted)
            {
                List<string> tmp = new List<string>();
                foreach (string s in l)
                {
                    if (validValves.Contains(s.ToUpper()))
                        tmp.Add(s.ToUpper());
                }
                
                ret.Add(tmp);
            }

            return ret;
        }

        private void FetchRestricted_Click(object sender, RoutedEventArgs e)
        {
            _connection.GetRestrictedValvesAsync(CommandPriority.lmoc, "LMOC");
            _connection.GetRestrictedValvesAsync(CommandPriority.music, "music");
            _connection.GetRestrictedValvesAsync(CommandPriority.kiosk, "kiosk");
            _connection.GetRestrictedValvesAsync(CommandPriority.music, "phone");
            _connection.GetRestrictedValvesAsync(CommandPriority.bollards, "bollards");
            _connection.GetRestrictedValvesAsync(CommandPriority.patterns, "patterns");
        }

        private void ValveButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;
            String valve = b.Content.ToString();
            List<string> cmd = new List<string>();
            cmd.Add(valve);

            if (_on.Contains(valve))
            {
                _connection.SendCommandAsync("OFF", cmd, CommandPriority.phone, null);
                _off.Add(valve);
                _on.Remove(valve);

                MainViewModel.SetRectColor(Colors.Red, valve);
            }
            else
            {
                _connection.SendCommandAsync("ON", cmd, CommandPriority.phone, null);
                _off.Remove(valve);
                _on.Add(valve);

                MainViewModel.SetRectColor(Color.FromArgb(255, 0, 205, 102), valve);
            }
        }

        public static void Disconnect()
        {
            _connection.CloseAsync();
        }

        private void RestrictedValve_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            MainViewModel.UpdateText(tb);
        }
    }
}